package com.example.zwh.computershop.login;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.zwh.computershop.Home_Activity;
import com.example.zwh.computershop.Myapp;
import com.example.zwh.computershop.R;
import com.example.zwh.computershop.androidUtil.ActivityCollector;
import com.example.zwh.computershop.handleDataClient.handleData;
import com.example.zwh.computershop.sqlModel.User;

import java.util.regex.Pattern;


public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText uname;
    private EditText pswone;
    private EditText pswtwo;
    private Button regis;
    private Myapp myapp;

    private String em;
    private String pso;
    private String pst;

    private Toast toast;
    private Intent intent;

    private User user;
    private int state;

    Toolbar toolbar;
    ImageView reg_img_name, reg_img_pwdone, reg_img_pwdtwo;
    LinearLayout reg_name_layout, reg_pwd_layout, reg_sure_layout;
    String regAccountString;
    public static final String REGEX_EMAIL = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        toolbar = (Toolbar) findViewById(R.id.toolBar);
        toolbar.setTitle("");

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ActivityCollector.addActivity(this);
        ininView();
        initEvent();
    }

    public void ininView() {
        uname = (EditText) findViewById(R.id.uname);
        pswone = (EditText) findViewById(R.id.pswone);
        pswtwo = (EditText) findViewById(R.id.pswtwo);
        regis = (Button) findViewById(R.id.register);

        reg_img_name = findViewById(R.id.iv_register_name);
        reg_img_pwdone = findViewById(R.id.iv_register_pwd);
        reg_img_pwdtwo = findViewById(R.id.iv_register_pwdtwo);

        reg_name_layout = findViewById(R.id.reg_name);
        reg_pwd_layout = findViewById(R.id.reg_pwd);
        reg_sure_layout = findViewById(R.id.reg_sure_pwd);

        myapp = (Myapp) getApplication();
        user = new User();
    }

    public void initEvent() {
        regis.setOnClickListener(this);
        uname.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    setNoSelected();
                    reg_name_layout.setSelected(true);
                    reg_img_name.setSelected(true);
                }
            }

        });

        pswone.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                setNoSelected();
                reg_pwd_layout.setSelected(true);
                reg_img_pwdone.setSelected(true);
            }
        });

        pswtwo.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                setNoSelected();
                reg_sure_layout.setSelected(true);
                reg_img_pwdtwo.setSelected(true);
            }
        });

        uname.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                inputIsOK();
            }
        });
        pswone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                inputIsOK();
            }
        });
        pswtwo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                inputIsOK();
            }
        });
        regis.setEnabled(false);

    }

    void setNoSelected() {
        reg_img_name.setSelected(false);
        reg_img_pwdone.setSelected(false);
        reg_img_pwdtwo.setSelected(false);
        reg_name_layout.setSelected(false);
        reg_pwd_layout.setSelected(false);
        reg_sure_layout.setSelected(false);
    }

    //判断输入是否合法，合法则登录按钮可用。
    private void inputIsOK() {
        regAccountString = uname.getText().toString().trim();
        String passwordString = pswone.getText().toString().trim();
        String surePasswordString = pswtwo.getText().toString().trim();
        if (regAccountString.length() > 5 && passwordString.length() > 5 && surePasswordString.length() > 5) {
            regis.setEnabled(true);
            regis.setBackgroundResource(R.drawable.login_actived);
            return;
        }
        regis.setEnabled(false);
        regis.setBackgroundResource(R.drawable.login_common);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register:
                em = uname.getText().toString();
                pso = pswone.getText().toString();
                pst = pswtwo.getText().toString();

                if (!Pattern.matches(REGEX_EMAIL, regAccountString)) {
                    Toast.makeText(this, "请输入正确的邮箱", Toast.LENGTH_SHORT).show();
                } else {
                    if (pso.compareTo(pst) != 0) {
                        toast.makeText(RegisterActivity.this, "密码不一致", Toast.LENGTH_SHORT).show();
                    } else {
                        user.setEmail(em);
                        user.setPassword(pso);
                        register(user);
                        if (state == 2) {
                            toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                            myapp.setUser(user);
                            myapp.setLoState(1);
                            intent = new Intent(RegisterActivity.this, Home_Activity.class);
                            startActivity(intent);
                            finish();
                        } else if (state == 1) {
                            toast.makeText(RegisterActivity.this, "账户已存在", Toast.LENGTH_SHORT).show();
                        } else {
                            toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                        }
                    }
                }
        }
    }

    public void register(User user) {
        Thread t = new Thread(new registerThread());
        t.start();
        try {
            t.join();
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }

    class registerThread extends Thread {
        @Override
        public void run() {
            state = handleData.handleRegister(user);
            user.printInfo();
        }
    }
}
